#include <new>
#include <windows.h>

#include "adapter_lock.h"

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
 	HANDLE mutex_handle = CreateMutexW(NULL, FALSE, L"Adapter_Lock");
 	if (NULL == mutex_handle)
 		return 1;
 
 	if (GetLastError() == ERROR_ALREADY_EXISTS) 
 		return 2;
 
 	char var[0xff] = { 0 };
 	if (0 == GetEnvironmentVariableA("ServiceProcessId", var, sizeof(var)))
 		return 3;

 	DWORD proc_id = atoi(var);
 
 	HANDLE parent_proc_handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, proc_id);
 	if (NULL == parent_proc_handle)
		return 4;

	AdapterLock adapter_lock;
	adapter_lock.LoadWhiteList();
	while (true)
	{
		adapter_lock.CheckAndDisableAdapters();
		if (WAIT_OBJECT_0  == WaitForSingleObject(parent_proc_handle, 5000))
		{
			DWORD dexit_code = 0;
			GetExitCodeProcess(parent_proc_handle, &dexit_code);
			if (dexit_code != STILL_ACTIVE)
				break;
		}
	}

	CloseHandle(mutex_handle);
	return 0;
}